# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.addons.dm_base.utils import field_boolean_name_cn as boolean_name,field_selection_name as selection_name
from openerp.addons.dm_base.utils import dtstr_utc2local
from openerp.addons.dm_rubylong import get_rubylong_fields_xml,get_rublong_data_url
from openerp.osv import fields, osv
from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT

class purchase_defect_order(osv.osv):
    _inherit="purchase.defect.order"
    
    def _get_rubylong_xml(self, cr, uid, ids, field_names, args, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        res = {}
        for order_id in ids:
            res[order_id] = ''
        for order in self.browse(cr, uid, ids, context=context):
            #当前对象的xml
            data_xml = ''
            #header data
            order_fields = [
                        ('company_id.logo','company_logo'),
                        ('company_id.name','company_id_name'),
#                        #公司地址
                        ('company_id.street','company_id_street'),
                        ('company_id.street2','company_id_street2'),
                        ('company_id.city','company_id_city'),
                        ('company_id.state_id.name','company_id_state'),
                        ('company_id.country_id.name','company_id_country_name'),
                        #电话,传真
                        ('company_id.phone','company_id_phone'),
                        ('company_id.fax','company_id_fax'),

                        'name',
                        ('purchase_id.name','purchase_id'),
                        ('move_id.qc_qty','qc_qty'),
                        ('move_id.qc_qty_check','qc_qty_check'),
                        ('move_id.qc_qty_defect','qc_qty_defect'),
                        
                        ('move_id.qc_solution','qc_solution'),
                        'note_purchase',
                        'note_engineer',
                        'note_quality',
                        'note_mrp',
                        'note_super',
                        
                        'qty_receive',
                        'qty_return',
                        ('id','order_id')
                        ]
            product_id = self.pool.get('product.product').name_get(cr, uid, [order.product_id.id])[0][1]
            partner_id = self.pool.get('res.partner').name_get(cr, uid, [order.partner_id.id])[0][1]
            qc_defect_id = ','.join([qc_defect.name for qc_defect in order.move_id.qc_defect_id])
            deal_method = selection_name(self, cr, uid, self._name, 'deal_method', order.deal_method, context=context)
            qc_date = order.move_id.qc_date and dtstr_utc2local(cr, uid,order.move_id.qc_date,context=context) or ''
            order_fields += [{'product_id':product_id}, 
                             {'partner_id':partner_id},
                             {'qc_defect_id':qc_defect_id},
                             {'deal_method':deal_method},
                             {'qc_date':qc_date}
                             ]

            #创建日期,创建人
            create_date = hasattr(order,'create_date') and (fields.datetime.context_timestamp(cr, uid,
                                                        timestamp=datetime.strptime(order.create_date, DEFAULT_SERVER_DATETIME_FORMAT),
                                                         context=context)) or ''
            create_uid = hasattr(order,'create_uid') and order.create_uid.name or ''
            order_fields += [{'create_date':create_date},{'create_uid':create_uid}]
            data_xml += get_rubylong_fields_xml(order, 'header', order_fields)
            
            #write data to file            
            res[order.id] = get_rublong_data_url(order,data_xml,cr.dbname)
            
        return res            

    _columns = {
            'rubylong_xml_file':fields.function(_get_rubylong_xml, type='text', string='Rubylong xml'),
            }
    
#po_super.STATE_SELECTION = STATE_SELECTION_PO  